package com.elitel.frame.web.cfgdbinfo.controller;

import com.elitel.common.core.domain.JsonResult;
import com.elitel.frame.web.cfgdbinfo.entity.CfgDbInfo;
import com.elitel.frame.web.cfgdbinfo.entity.DbTreeMap;
import com.elitel.frame.web.cfgdbinfo.service.CfgDbInfoService;
import com.elitel.frame.web.common.controller.ApiController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.elitel.frame.web.cfgdbinfo.service.impl.CfgDbInfoServiceImpl.DisplayPassword;

/**
 * 数据库连接表(CfgDb)表控制层
 *
 * @author makejava
 * @since 2025-05-19 15:27:57
 */
@RestController
@RequestMapping("cfgDbInfo")
public class CfgDbInfoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CfgDbInfoService cfgDbInfoService;


    @GetMapping("list")
    public JsonResult<?> list(CfgDbInfo cfgDbInfo) {
        // 联合查询了表cfg_db和sys_project, 主要是为了兼容老逻辑
        // 老的表结构还算合理,就用原来的字段返回就行
        List<CfgDbInfo> joinList = cfgDbInfoService.getJoinList(cfgDbInfo);
        return JsonResult.success(joinList);
    }

    @GetMapping("listByPage")
    public JsonResult<?> listByPage(Integer pageNum,Integer  pageSize,String seachTxt) {
        startPage();
        List<CfgDbInfo> list = cfgDbInfoService.queryList(pageNum,pageSize,seachTxt);
        return getDataTable(list);
    }

    @GetMapping("getTree")
    public JsonResult<?> getTree(String dbType, String dbK) {
        List<DbTreeMap> list = cfgDbInfoService.getAll(dbType,dbK);
        return JsonResult.success(list);
    }

    @GetMapping("getItems")
    public JsonResult<?> getItems(String db,String dbk,Integer pageNum,Integer pageSize) {
        HashMap<String, Object> list = cfgDbInfoService.getItems(db,dbk,pageNum,pageSize);
        return JsonResult.success(list);
    }

    @PostMapping("save")
    public JsonResult<?> save(@RequestBody CfgDbInfo cfgDbInfo) {
        String dbKey = cfgDbInfoService.save(cfgDbInfo);
        return JsonResult.success(dbKey);
    }

    /**
     * @return CfgDb实体
     * @throws @Title: selectCfgDbBydbKey @Description: TODO(通过主键dbKey查询数据库链接配置表信息)
     * @author guoyanfei
     */
    @GetMapping("/selectByDbKey")
    @ResponseBody
    public JsonResult<CfgDbInfo> selectByDbKey(String dbKey) {
        CfgDbInfo cfgDb = cfgDbInfoService.selectByDbKey(dbKey);
        if (cfgDb != null && cfgDb.getDbPwd() != null) {
            cfgDb.setDbPwd(DisplayPassword);
        }
        return JsonResult.success(cfgDb);
    }


    @PostMapping("deleteDbConfigInfo")
    public JsonResult<?> deleteDbConfigInfo(@RequestBody CfgDbInfo cfgDbInfo) {
        try {
            cfgDbInfoService.deleteDbConfigInfo(cfgDbInfo.getDbKey());
            return JsonResult.success();
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PostMapping("testConnection")
    public JsonResult<?> testConnection(@RequestBody CfgDbInfo cfgDbInfo) {
        try {
            boolean result = cfgDbInfoService.testConnection(cfgDbInfo);
            return result ? JsonResult.success() : JsonResult.error("连接失败, 请检查连接信息是否有误");
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}

